package com.cool.controller.addcontroller;

import com.cool.bo.addbo.SelectBig;
import com.cool.bo.addbo.SelectBigBo;
import com.cool.bo.addbo.classbo.ClassBo;
import com.cool.bo.merbo.Mer;
import com.cool.common.CommonResult;
import com.cool.common.ResponseCode;
import com.cool.po.bigclasspo.BigClassPo;
import com.cool.po.brand.BrandPo;
import com.cool.po.shoppo.ShopPo;
import com.cool.po.smallclasspo.SmallClassPo;
import com.cool.service.addshop.AddService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 商品信息添加控制层
 */
@RestController
@RequestMapping("add")
public class AddController {
    @Autowired
    private AddService addService;
    /**
     * 查询出来所有门店名称
     */
    @RequestMapping("getshopName")
    CommonResult getshopName(){
        List<ShopPo> list = addService.getshopName();
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("新增成功!").setData(list);
    }
    /**
     * 查询出所有大类
     */
    @RequestMapping("getbigClass")
    CommonResult getbigClass(){
        List<BigClassPo> list = addService.getBigClass();
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("新增成功!").setData(list);
    }
    /**
     * 根据大类id查询出关联的所有小类
     */
    @RequestMapping("getsmallClass")
    CommonResult getsmallClass(@RequestBody SelectBigBo selectBigBo){
        System.out.println(selectBigBo);
        List<SmallClassPo> list = addService.getSmallClass(selectBigBo);
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("新增成功!").setData(list);
    }
    /**
     * 查询出所有的商品品牌
     */
    @RequestMapping("getBrand")
    CommonResult getBrand(){
        List<BrandPo> list = addService.getBrand();
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("新增成功!").setData(list);
    }
    /**
     * 表单新增商品所有数据
     */
    @RequestMapping("addMer")
    CommonResult addMer(@RequestBody Mer mer){
        addService.addMer(mer);
        //给商品信息和商品大类中间表添加数据:
        addService.addmerBig(mer);
        //新增商品时给商品品牌中间表添加信息
        addService.addbrand(mer);
        //根据新增商品的主键查询出分类信息
        List<Map<String, Object>> list = addService.getBigSmall(mer);
        String str=list.get(0).get("bigclass_name")+">"+list.get(0).get("smallclass_name");
        //再将分页信息添加到对象中
        ClassBo classBo = new ClassBo();
        classBo.setMerid(mer.getShopInfo().getMerId());
        classBo.setStr(str);
        addService.addClass(classBo);
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("新增成功!");
    }
    /**
     * 查询出商品所有数据
     */
    @RequestMapping("getMer")
    CommonResult getMer(){
        //查询出所有信息
        List<Map<String, Object>> list = addService.getMer();
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("新增成功!").setData(list);
    }
}
